package com.wangyf.thread.c_019_Exchanger;

import java.util.concurrent.Exchanger;

/**
 * 测试Exchanger 可以用于线程通信
 * Created by feng on 2021/1/13.
 * 等待线程会被阻塞
 */
public class T {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();
        new Thread(() -> {
            String s = "aaa";
            try {
                exchanger.exchange(s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("T1   "+s);
        }).start();
        new Thread(() -> {
            String s = "111";
            try {
                exchanger.exchange(s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("T2   "+s);
        }).start();
    }
}
